package com.agadmin.manage.handle.software.checkKey;

import com.agadmin.entity.software.Software;
import com.agadmin.manage.vo.reponse.software.CheckKeyResponse;
import com.agadmin.mapper.SoftwareMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;



@Service
public class CheckKeyValid {


    @Resource
    SoftwareMapper softwareMapper;
    public CheckKeyResponse verifyKey(String key){
        CheckKeyResponse res=new CheckKeyResponse();
        LambdaQueryWrapper<Software> qw=new LambdaQueryWrapper();
        qw.eq(Software::getSoftwareKey,key);
        Software software = softwareMapper.selectOne(qw);
        if(software==null){
            res.setCode(402);
            res.setError("not found key");
            res.setCheckResult(false);
            return res;
        }
        long timestamp = System.currentTimeMillis();
        Long expireDate = software.getExpireDate();


        if(timestamp>expireDate){
            res.setCode(405);
            res.setError("the key has expired");
            res.setCheckResult(false);
            return res;
        }

        res.setCode(200);
        res.setMsg("available");
        res.setCheckResult(true);
        return res;
    }

}
